$(function() {
	question_list.initial();
});

var question_list = {
	initial : function initial() {
		this.bindChangeSearchParam();
		this.bindDelete();
		this.bindUpdate();
		this.bindUpdateField();
	},
	
	bindChangeSearchParam : function bindChangeSearchParam(){
		$("#question-filter dl dd span").click(function(){
			if($(this).hasClass("label"))return false;
			var genrateParamOld = question_list.genrateParamOld();
			if($(this).parent().parent().attr("id") == "question-filter-field" ){
				genrateParamOld.field = $(this).data("id");
				question_list.redirectUrl(genrateParamOld);
				
			}
		});
		
		$(".pagination li a").click(function(){
			var pageId = $(this).data("id");
			if(pageId==null||pageId=="")return false;
			var genrateParamOld = question_list.genrateParamOld();
			genrateParamOld.page = pageId;
			question_list.redirectUrl(genrateParamOld);
			
		});
	},
	
	genrateParamOld :function genrateParamOld(){
		
		var field = $("#question-filter-field dd .label").data("id");
		var page = 1;
		var data = new Object();
		data.field = field;
		data.page = page;
		
		return data;
	},
	redirectUrl : function(newparam) {
		var paramurl = newparam.field;
		paramurl = paramurl + "-" + newparam.page;

		document.location.href = document.getElementsByTagName('base')[0].href
				+ 'admin/point-list-' + paramurl;
	},
	bindDelete : function bindDelete(){
		$(".delete-btn").click(function(){
			$.ajax({
				headers : {
					'Accept' : 'application/json',
					'Content-Type' : 'application/json'
				},
				type : "GET",
				url : "admin/delete-point-" + $(this).data("id"),
				success : function(message, tst, jqXHR) {
					if (!util.checkSessionOut(jqXHR))
						return false;
					if (message.result == "success") {
						util.success("删除成功", function(){
							window.location.reload();
						});
					} else {
						util.error("操作失败请稍后尝试:" + message.result);
					}

				},
				error : function(jqXHR, textStatus) {
					util.error("操作失败请稍后尝试");
				}
			});
			return false;
		});
	},
	bindUpdate : function bindUpdate(){
		$(".update-btn").click(function(){
			var point_id = $(this).data("id");
			$("#change-property-modal").modal({backdrop:true,keyboard:true});
			var tr = $(this).parent().parent();
			var point_name = tr.find(".td-point-name").text();
			var point_memo = tr.find(".td-point-memo").text();
			var field_name = tr.find(".td-field-name").text();
			//设置对应题库
			$("#exampaper-filed-select option").each(function(i,n){
				if($(n).text() == field_name) {
					$(n).attr("selected", true);
				}
			});
			$(".add-update-pointname input").val(point_name);
			$(".add-update-memo input").val(point_memo);
			$("#add-update-pointid").text(point_id);
		});
	},
	bindUpdateField : function bindUpdateField(){
		$("#update-point-btn").click(function(){
			var verify_result = question_list.verifyInput();
			var point_id = $("#add-update-pointid").text();
			var field_id = $("#exampaper-filed-select").val();
			if (verify_result) {
				var data = new Object();
				data.pointId = point_id;
				data.fieldId = field_id;
				data.pointName = $(".add-update-pointname input").val();
				data.memo = $(".add-update-memo input").val();
				$.ajax({
					headers : {
						'Accept' : 'application/json',
						'Content-Type' : 'application/json'
					},
					type : "POST",
					url : "admin/point-update",
					data : JSON.stringify(data),
					success : function(message, tst, jqXHR) {
						console.log("修改知识点返回："+message);
						if (!util.checkSessionOut(jqXHR))
							return false;
						if (message.result == "success") {
							util.success("修改成功", function(){
								window.location.reload();
							});
						} else {
							util.error(message.result);
						}

					},
					error : function(jqXHR, textStatus) {
						util.error("网络错误，请稍后尝试！");
					}
				});
			}
		});
	},
	verifyInput : function verifyInput() {
		$(".form-message").empty();
		$(".has-error").removeClass("has-error");
		var result = true;
		var r_checkName = question_list.checkName();
		var r_checkField = question_list.checkField();
		result = r_checkName && r_checkField;
		return result;
	},	
	checkName : function checkName() {
		var name = $(".add-update-pointname input").val();
		if (name == "") {
			$(".add-update-pointname .form-message").text("请输入知识点名称");
			$(".add-update-pointname input").focus();
			$(".add-update-pointname input").addClass("has-error");
			return false;
		} else if (name.length > 40 || name.length < 4) {
			$(".add-update-pointname .form-message").text("请保持在4-40个字符以内");
			$(".add-update-pointname input").focus();
			$(".add-update-pointname input").addClass("has-error");
			return false;
		} else {
			return true;
		}
	},
	checkField : function checkField(){
		var val = $("#exampaper-filed-select").val();
		if(val == -1 || val == "-1") {
			$(".add-update-filed .form-message").text("请选择题库");
			$(".add-update-filed select").focus();
			$(".add-update-filed select").addClass("has-error");
			return false;
		} else
			return true;
	}
};